package cn.edu.njuit.children.service.impl;

import cn.edu.njuit.children.entity.Role;
import cn.edu.njuit.children.entity.UserAccount;
import cn.edu.njuit.children.entity.model.ChildrenModel;
import cn.edu.njuit.children.mapper.RoleMapper;
import cn.edu.njuit.children.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.njuit.children.entity.Children;
import cn.edu.njuit.children.mapper.ChildrenMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Administrator
 * @description 针对表【sys_children】的数据库操作Service实现
 * @createDate 2022-11-29 08:58:00
 */
@Service
public class ChildrenServiceImpl extends ServiceImpl<ChildrenMapper, Children>
        implements ChildrenService {

    @Autowired
    UserService userService;


    @Autowired
    RoleMapper roleMapper;
    @Autowired
    BaiduAiService baiduAiService;

    @Override
    public boolean addChildren(ChildrenModel o) {
        String phone = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        UserAccount account = userService.getAccount(phone);
        Children children = new Children();
        if (o.getCdAvatar() != null) {
            String fileKey = UUID.randomUUID().toString();
            String fileName = o.getCdAvatar().getName();
            if (fileName.indexOf(".") > 0) {
                String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
                fileKey += extName;
            }
            try {
                baiduAiService.putFile(fileKey, o.getCdAvatar().getInputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
            children.setCdAvatar("https://web-lesson.bj.bcebos.com/children/" + fileKey);
        }
        children.setAcatar(new Date());
        children.setUserId(account.getId());
        if (o.getCdBirthday() != null) {
            Date date = new Date(o.getCdBirthday());
            children.setCdBirthday(date);
        }
        children.setCdNickname(o.getCdNickname());
        children.setCdGender(o.getCdGender());

        return this.save(children);
    }

    @Override
    public boolean upChildren(ChildrenModel o) throws Exception {
        String phone = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        UserAccount account = userService.getAccount(phone);

        Children byId = this.getById(o.getId());
        if (o.getId() != null && byId != null) {
            if (!byId.getUserId().equals(account.getId())) {
                List<Role> roleList = roleMapper.findByPhone(phone);
                boolean flag = false;
                for (Role role : roleList) {
                    if ("role_admin".equals(role.getRoleName())) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    throw new Exception("无权限");
                }
            }
        }

        Children children = new Children();
        if (o.getCdAvatar() != null) {
            String fileKey = UUID.randomUUID().toString();
            String fileName = o.getCdAvatar().getName();
            if (fileName.indexOf(".") > 0) {
                String extName = fileName.substring(fileName.lastIndexOf(".") - 1);
                fileKey += extName;
            }
            try {
                baiduAiService.putFile(fileKey, o.getCdAvatar().getInputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
            children.setCdAvatar("https://web-lesson.bj.bcebos.com/children/" + fileKey);
        }
        if (o.getCdBirthday() != null) {
            Date date = new Date(o.getCdBirthday());
            children.setCdBirthday(date);
        }
        children.setCdNickname(o.getCdNickname());
        children.setCdGender(o.getCdGender());
        children.setId(o.getId());

        return this.updateById(children);
    }

    @Override
    public boolean deleteChildren(Long id) throws Exception {

        String phone = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        UserAccount account = userService.getAccount(phone);

        Children byId = this.getById(id);
        if (id != null && byId != null) {
            if (!byId.getUserId().equals(account.getId())) {
                List<Role> roleList = roleMapper.findByPhone(phone);
                boolean flag = false;
                for (Role role : roleList) {
                    if ("role_admin".equals(role.getRoleName())) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    throw new Exception("无权限");
                }
            }
        }

        return this.removeById(id);
    }
}




